package com.findviewbydream.evil.helloyouth.Fragment.HomeFragmentContent;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.findviewbydream.evil.helloyouth.Data.NetData;
import com.findviewbydream.evil.helloyouth.R;

/********************************************
 * 工程名：HelloYouth
 * 包名：com.findviewbydream.evil.helloyouth.Fragment
 * 时间：16-6-7 下午11:21
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：中外交流界面
 ********************************************/
public class CommunionFragment extends Fragment
{
    private View view;
    private static CommunionFragment communionFragment;

    private WebView webView;
    private ProgressBar progressBar;

    /**
     * 入口函数
     */
    public static CommunionFragment newInstance()
    {
        if (communionFragment == null)
            communionFragment = new CommunionFragment();
        return communionFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.home_communionfragment, container, false);

        initView(view);

        setWebView();

        return view;
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view)
    {
        webView = (WebView) view.findViewById(R.id.webView);
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    }


    private void setWebView()
    {
        WebSettings webSettings = webView.getSettings();

        /*设置适应屏幕*/
        {
            webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
            webSettings.setLoadWithOverviewMode(true);
        }
        /*支持缩放*/
        {
            webSettings.setJavaScriptEnabled(true);
            webSettings.setBuiltInZoomControls(true);
            webSettings.setSupportZoom(true);
        }
        /*支持手动输入*/
        {
            //webView.requestFocusFromTouch();
        }

        /*优先使用缓存*/
        {
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            // 开启 DOM storage API 功能
            webSettings.setDomStorageEnabled(true);
        }


        webView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
                if (newProgress == 100)
                {
                    progressBar.setVisibility(View.INVISIBLE);
                }
                else
                {
                    progressBar.setProgress(newProgress);
                }
            }
        });

        /*在当前页面打开新的链接*/
        webView.loadUrl(NetData.COMMUNITY_URL);
        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                progressBar.setVisibility(View.VISIBLE);
                return true;
            }
        });
    }


    @Override
    public void onDestroy()
    {
        super.onDestroy();
        communionFragment = null;
    }
}
